package com.ld.control.business.equipment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.equipment.mapper.EquipmentMethodDebugParamMapper;
import com.ld.control.business.equipment.mapper.EquipmentMethodParamTmpMapper;
import com.ld.control.business.equipment.mapper.EquipmentMethodTmpMapper;
import com.ld.control.business.equipment.model.EquipmentMethodDebugParamModel;
import com.ld.control.business.equipment.model.EquipmentMethodParamTmpModel;
import com.ld.control.business.equipment.model.EquipmentMethodTmpModel;
import com.ld.control.business.equipment.service.EquipmentMethodTmpService;
import com.ld.control.business.equipment.service.EquipmentService;
import com.ld.control.business.equipment.vo.external.EquipmentCommandSendParamReqVo;
import com.ld.control.business.equipment.vo.req.EquipmentMethodTmpReqVo;
import com.ld.control.business.equipment.vo.req.MethodDebugParamReqVo;
import com.ld.control.business.equipment.vo.req.MethodDebugReqVo;
import com.ld.control.business.equipment.vo.resp.EquipmentMethodTmpRespVo;
import com.ld.control.business.equipment.vo.resp.EquipmentRespVo;
import com.ld.control.business.equipment.vo.resp.MethodDebugRespVo;
import com.ld.control.business.process.model.ProcessNodeExampleModel;
import com.ld.control.business.process.model.ProcessNodeExampleRunningParamModel;
import com.ld.control.business.process.model.ProcessNodeParamExampleModel;
import com.ld.control.common.vo.EquipmentSystemMethodVo;
import com.ld.control.utils.EquipmentCommandSendUtils;
import com.ld.control.utils.EquipmentSystemMethodUtils;
import com.ld.control.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 设备方发表 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-04-17
 */
@Slf4j
@Service
public class EquipmentMethodTmpServiceImpl extends ServiceImpl<EquipmentMethodTmpMapper, EquipmentMethodTmpModel> implements EquipmentMethodTmpService {

    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private EquipmentCommandSendUtils equipmentCommandSendUtils;

    @Autowired
    private EquipmentMethodTmpMapper equipmentMethodTmpMapper;

    @Autowired
    private EquipmentMethodParamTmpMapper equipmentMethodParamTmpMapper;

    @Autowired
    private EquipmentMethodDebugParamMapper equipmentMethodDebugParamMapper;

    @Autowired
    private EquipmentSystemMethodUtils equipmentSystemMethodUtils;

    @Autowired
    private EquipmentService equipmentService;


    @Override
    public void add(EquipmentMethodTmpReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        EquipmentMethodTmpModel equipmentMethodTmpModel = new EquipmentMethodTmpModel();
        BeanUtils.copyProperties(reqVo, equipmentMethodTmpModel);
        equipmentMethodTmpModel.setCreateBy(currentUserAccountId);
        equipmentMethodTmpModel.setCreateTime(new Date());
        equipmentMethodTmpMapper.insert(equipmentMethodTmpModel);

    }

    @Override
    public void update(EquipmentMethodTmpReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        EquipmentMethodTmpModel equipmentMethodTmpModel = new EquipmentMethodTmpModel();
        BeanUtils.copyProperties(reqVo, equipmentMethodTmpModel);
        equipmentMethodTmpModel.setUpdateBy(currentUserAccountId);
        equipmentMethodTmpModel.setUpdateTime(new Date());
        equipmentMethodTmpMapper.updateById(equipmentMethodTmpModel);
    }

    @Override
    public void deleteById(Long id) {
        equipmentMethodTmpMapper.deleteById(id);
        Map<String, Object> columnMa = new HashMap<>();
        columnMa.put(EquipmentMethodParamTmpModel.EQUIPMENT_METHOD_TMP_ID, id);
        equipmentMethodParamTmpMapper.deleteByMap(columnMa);
    }

    @Override
    public EquipmentMethodTmpRespVo getById(Long id) {
        EquipmentMethodTmpRespVo equipmentMethodTmpRespVo = new EquipmentMethodTmpRespVo();
        EquipmentMethodTmpModel equipmentMethodTmpModel = equipmentMethodTmpMapper.selectById(id);
        BeanUtils.copyProperties(equipmentMethodTmpModel, equipmentMethodTmpRespVo);
        return equipmentMethodTmpRespVo;
    }

    @Override
    public List<EquipmentMethodTmpRespVo> getList(Long equipmentId, Integer debugFlag) {
        return equipmentMethodTmpMapper.selectListByEquipmentId(equipmentId, debugFlag);
    }

    @Override
    public MethodDebugRespVo sendDebugMethod(MethodDebugReqVo reqVo) {
        log.debug("sendDebugMethod reqVo:{}", JSONObject.toJSONString(reqVo));
        // 清空原来参数
        Map<String, Object> columnMap = new HashMap<>();
        List<ProcessNodeParamExampleModel> processNodeParamExampleModelList = new ArrayList<>();
        columnMap.put(EquipmentMethodDebugParamModel.EQUIPMENT_METHOD_TMP_ID, reqVo.getEquipmentMethodTmpId());
        equipmentMethodDebugParamMapper.deleteByMap(columnMap);

        ProcessNodeParamExampleModel processNodeParamExampleModel = null;
        for (MethodDebugParamReqVo data : reqVo.getMethodDebugParamReqVoList()) {
            EquipmentMethodDebugParamModel equipmentMethodDebugParamModel = new EquipmentMethodDebugParamModel();
            equipmentMethodDebugParamModel.setEquipmentMethodTmpId(reqVo.getEquipmentMethodTmpId());
            equipmentMethodDebugParamModel.setUseType(data.getUseType());
            equipmentMethodDebugParamModel.setParamType(data.getParamType());
            equipmentMethodDebugParamModel.setName(data.getName());
            equipmentMethodDebugParamModel.setValue(data.getValue());
            equipmentMethodDebugParamMapper.insert(equipmentMethodDebugParamModel);

            // 添加内部方法调试的参数
            processNodeParamExampleModel = new ProcessNodeParamExampleModel();
            processNodeParamExampleModel.setName(data.getName());
            processNodeParamExampleModel.setValue(data.getValue());
            processNodeParamExampleModelList.add(processNodeParamExampleModel);

        }
        EquipmentMethodTmpRespVo equipmentMethodTmpRespVo = getById(reqVo.getEquipmentMethodTmpId());
        String equipmentMethodTmpName = equipmentMethodTmpRespVo.getName();
        Integer methodType = equipmentMethodTmpRespVo.getMethodType();
        Long equipmentId = equipmentMethodTmpRespVo.getEquipmentId();

        EquipmentRespVo equipmentRespVo = equipmentService.getById(equipmentId);
        String equipmentCode = equipmentRespVo.getCode();

        EquipmentCommandSendParamReqVo equipmentCommandSendParamReqVo = new EquipmentCommandSendParamReqVo();
        if (methodType == 0) {
            equipmentCommandSendParamReqVo = equipmentCommandSendUtils.generateMethodDebugParamReqVo(equipmentId,
                    equipmentCode, equipmentMethodTmpName, reqVo.getMethodDebugParamReqVoList());
        }
        if (methodType == 1) {
            ProcessNodeExampleModel processNodeExampleModel = new ProcessNodeExampleModel();
            processNodeExampleModel.setNodeName(equipmentMethodTmpName);
            EquipmentSystemMethodVo equipmentSystemMethodVo = equipmentSystemMethodUtils.equipmentSystemMethodMapping(0L, processNodeExampleModel, processNodeParamExampleModelList);
            String sendEquipmentMethodName = equipmentSystemMethodVo.getEquipmentMethodName();
            List<ProcessNodeExampleRunningParamModel> processNodeExampleRunningParamModelList = equipmentSystemMethodVo.getProcessNodeExampleRunningParamModelList();
            List<MethodDebugParamReqVo> methodDebugParamReqVoList = new ArrayList<>();
            for (ProcessNodeExampleRunningParamModel data : processNodeExampleRunningParamModelList) {
                MethodDebugParamReqVo methodDebugParamReqVo = new MethodDebugParamReqVo();
                methodDebugParamReqVo.setName(data.getSendMethodParamName());
                methodDebugParamReqVo.setUseType(0);
                methodDebugParamReqVo.setParamType(data.getParamType());
                methodDebugParamReqVo.setValue(data.getSendMethodParamValue());
                methodDebugParamReqVoList.add(methodDebugParamReqVo);
            }
            equipmentCommandSendParamReqVo = equipmentCommandSendUtils.generateMethodDebugParamReqVo(equipmentId,
                    equipmentCode, sendEquipmentMethodName, methodDebugParamReqVoList);
        }
        String postResp = equipmentCommandSendUtils.sendEquipmentCommandDebug(equipmentCommandSendParamReqVo);
        return JSONObject.parseObject(postResp, MethodDebugRespVo.class);
    }

    @Override
    public void setErrorAlarm(String equipmentCode, String param) {
        log.info("错误告警消除");
        log.debug("setErrorAlarm equipmentCode:{} param:{}", equipmentCode, param);
       /* QueryWrapper<EquipmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(EquipmentModel.CODE, equipmentCode);
        queryWrapper.eq(EquipmentModel.DEL_FLAG, DeleteFlagEnum.NO.getCode());
        EquipmentModel equipmentModel = equipmentMapper.selectOne(queryWrapper);
        EquipmentCommandSendParamReqVo equipmentCommandSendParamReqVo = new EquipmentCommandSendParamReqVo();
        equipmentCommandSendParamReqVo.setTaskProcessNodeExampleId(0L);
        equipmentCommandSendParamReqVo.setEquipmentId(equipmentModel.getId());
        equipmentCommandSendParamReqVo.setEquipmentCode(equipmentModel.getCode());
        equipmentCommandSendParamReqVo.setMethodName("OpenLight");
        equipmentCommandSendParamReqVo.setMessageId("debug_" + equipmentModel.getCode() + "_OpenLight");
        JSONObject methodParam = new JSONObject();
        methodParam.put("channel", param);
        equipmentCommandSendParamReqVo.setMethodParams(methodParam);
        String postResp = equipmentCommandSendUtils.sendEquipmentCommandDebug(equipmentCommandSendParamReqVo);
        log.info("setErrorAlarm resp:{}", postResp);*/
    }
}
